package com.bpc.cs.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.bpc.cs.constant.Constant;
import com.bpc.cs.dao.IFactorDAO;
import com.bpc.cs.dto.FactorDTO;
import com.bpc.cs.managed.dvo.FactorDVO;
import com.bpc.cs.util.Message;
import com.bpc.cs.util.Util;

/**
 * @author   phan_tp
 * @datetime Dec 19, 2012 : 9:53:23 AM
 */

@Transactional(readOnly = false)
public class FactorService implements IFactorService {

	private IFactorDAO factorDAO;

	/**
	 * Get all Factor
	 * @return List<FactorDVO>
	 */
	@Override
	public List<FactorDVO> getAllFactor() {
		return Util.cvnFactorDTO2DVO(factorDAO.selectAllFactor());
	}

	/**
	 * Add new factor name
	 * @param String factorName
	 * @return Message
	 */
	@Override
	public Message addFactor(String factorName) {
		// Message to transfer data
		Message msg = new Message();

		// Call factor service to check factor is duplicated or not
		if (factorDAO.isDuplicateFactorName(factorName)) {
			// Factor Name is duplicated
			msg.setResultBoolean(false);

			// Set message Key to check and view message
			msg.setMessageKey(Constant.DUPLICATE_FACTOR_NAME);
		} else {
			factorDAO.insertFactor(new FactorDTO(factorName));
			msg.setResultBoolean(true);
		}

		return msg;
	}

	/**
	 * @return the factorDAO
	 */
	public IFactorDAO getFactorDAO() {
		return factorDAO;
	}

	/**
	 * @param factorDAO the factorDAO to set
	 */
	public void setFactorDAO(IFactorDAO factorDAO) {
		this.factorDAO = factorDAO;
	}


}
